package com.kexun.servlet.student;

import com.kexun.pojo.Student;
import com.kexun.service.StuService;
import com.kexun.service.impl.StuServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/ServletEditStudentInfo")
public class ServletEditStudentInfo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 1 接收参数
        Map<String, String[]> map = request.getParameterMap();
        Student student = new Student();

        try {
            BeanUtils.populate(student,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        StuService st = new StuServiceImpl();

        int i = st.update(student);
//        StuService stu = new StuServiceImpl();
//        ServletStudentInfo s = new ServletStudentInfo();
        if (i>0){
//            s.doPost(request,response);
            request.getRequestDispatcher("/ServletStudentInfo").forward(request,response);

        }





    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
